package cn.yuyiling.jelly.qa.controller;

import cn.yuyiling.jelly.kg.mongodb.entity.KnowledgePoint;
import cn.yuyiling.jelly.qa.api.QuestionService;
import cn.yuyiling.jelly.qa.dto.QuestionDetails;
import cn.yuyiling.jelly.qa.mongodb.entity.Question;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/questions")
public class QuestionController {

    private static final Logger logger = LoggerFactory.getLogger(QuestionController.class);

    @DubboReference(scope = "remote")
    private QuestionService questionService;

    @PostMapping
    public Question createQuestion(@RequestBody Question question) {
        return questionService.createQuestion(question);
    }

    @PutMapping("/update/{id}")
    public Question updateQuestion(@PathVariable String id, @RequestBody Question question) {
        return questionService.updateQuestion(id, question);
    }

    @PutMapping("/like/{id}")
    public Question likeQuestion(@PathVariable String id) {
        return questionService.likeQuestion(id);
    }

    @PutMapping("/dislike/{id}")
    public Question dislikeQuestion(@PathVariable String id) {
        return questionService.dislikeQuestion(id);
    }

    @DeleteMapping("/{id}")
    public void deleteQuestion(@PathVariable String id) {
        questionService.deleteQuestionAndRelatedData(id);
    }

    @GetMapping("/count/totalQuestions")
    public long getTotalQuestionCount() {
        return questionService.getTotalQuestionCount();
    }

    @GetMapping("/hottest")
    public List<Question> getHottestQuestions(@RequestParam(defaultValue = "1") int limit) {
        return questionService.getHottestQuestions(limit);
    }

    @GetMapping("/recommended")
    public List<Question> getRecommendedQuestions(@RequestParam List<String> knowledgePoints) {
        return questionService.getRecommendedQuestions(knowledgePoints);
    }

    @GetMapping("/selectByKnowledgePoint")
    public List<Question> getQuestionsByKnowledgePoint(@RequestParam String knowledgePoint) {
        return questionService.getQuestionsByKnowledgePoint(knowledgePoint);
    }

    @GetMapping("/{id}/details")
    public QuestionDetails getQuestionDetails(@PathVariable String id) {
        return questionService.getQuestionDetails(id);
    }

    @PostMapping("/add_knowledgePoint/{id}")
    public void addKnowledgePoint(@PathVariable String id, @RequestBody String knowledgePoint) {
        logger.info("Adding knowledgePoint to question with ID: {}, knowledgePoint: {}", id, knowledgePoint);
        questionService.addKnowledgePoint(id, knowledgePoint);
    }

    @GetMapping("/view/{id}")
    public Question viewQuestionById(@PathVariable String id) {
        logger.info("view Question by ID: {}", id);
        return questionService.viewQuestionById(id);
    }

    @GetMapping("/view/top/{count}")
    public List<String> viewQuestionRank(@PathVariable Integer count) {
        logger.info("view Question rank list top: {}", count);
        return questionService.get24HourTopViewQuestion(count);
    }

    /**
     * PB:
     * 注意在分布式部署的情况下，本代码可能被部署到多个节点（Pod），那么每个节点都会独立运行该定时任务，务必小心
     * 本示例多次执行该任务问题不大，如果存在多节点重复执行会有问题的情况下建议
     * 1）用redis加锁，避免多节点同时执行
     * 2）用其他定时任务机制保障该任务只被执行一次
     */
    //@Scheduled(cron = "0 0 * * * ?")
    @Scheduled(cron = "0/30 * * * * ?")
    public void refreshTop24hViewQuestionRanking() {
        questionService.refreshTop24hViewQuestionRanking();
    }


    @GetMapping("/like/{id}")
    public Question likeQuestionById(@PathVariable String id) {
        logger.info("like Question by ID: {}", id);
        return questionService.likeQuestionById(id);
    }

    @GetMapping("/popular/top/{count}")
    public List<String> popularQuestionRank(@PathVariable Integer count) {
        logger.info("popular Question rank list top: {}", count);
        return questionService.get24HourTopPopularQuestion(count);
    }

    /**
     * PB:
     * 注意在分布式部署的情况下，本代码可能被部署到多个节点（Pod），那么每个节点都会独立运行该定时任务，务必小心
     * 本示例多次执行该任务问题不大，如果存在多节点重复执行会有问题的情况下建议
     * 1）用redis加锁，避免多节点同时执行
     * 2）用其他定时任务机制保障该任务只被执行一次
     */
    //@Scheduled(cron = "0 0 * * * ?")
    @Scheduled(cron = "0/30 * * * * ?")
    public void refreshTop24hPopularQuestionRanking() {
        questionService.refreshTop24hPopularQuestionRanking();
    }
}